#include "TaskQueue.hpp"

namespace pc{

TaskQueue::TaskQueue(size_t queSize)
:_que()
,_queSize(queSize)
,_mutex()
,_notFull(_mutex)
,_notEmpty(_mutex)
{

}

TaskQueue::~TaskQueue(){

}

bool TaskQueue::empty() const{
    return 0 == _que.size();
}

bool TaskQueue::full() const{
    return _queSize == _que.size();
}

void TaskQueue::push(int elem){
    _mutex.lock();

    while(full()){
        _notFull.wait();
    }
    _que.push(elem);
    _notEmpty.notify();

    _mutex.unlock();
}

int TaskQueue::pop(){
    _mutex.lock();

    while(empty()){
        _notEmpty.wait();
    }
    int tmp = _que.front();
    _que.pop();
    _notFull.notify();

    _mutex.unlock();

    return tmp;
}

}//end of namespace pc
